package csy.view.memeda;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.inputmethodservice.Keyboard;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import csy.view.memeda.utils.DensityUtils;

/**
 * csy
 */

public class CirclePercentView extends View {

    private int mCircleColor;
    private int mArcColor;
    private int mArcWidth;
    private int mPercentTextColor;
    private int mPercentTextSize;
    private int mRadius;
    private Paint mCircleoutPaint;
    private Paint mCirclePaint;
    private Paint mArcPaint;
    private Paint mPercentTextPaint;
    private RectF mArcRectF;
    private Rect mTextBound;
    private OnClickListener mOnClickListener;
    //进度条的进度
    private float mCurPercent = 0.0f;
    float mXdown = 0;
    float mYdown = 1;
    //123321

    public void setCurPercent(float curPercent) {
        mCurPercent = 0.0f;
        ValueAnimator anim = ValueAnimator.ofFloat(mCurPercent, curPercent);
        anim.setDuration((long) (Math.abs(mCurPercent - curPercent) * 20));
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mCurPercent = (float) (Math.round(value * 10)) / 10;//四舍五入保留到小数点后两位
                invalidate();
            }
        });
        anim.start();
    }

    public void setOnCircleClickListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }


    public CirclePercentView(Context context) {
        this(context, null);
    }

    public CirclePercentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirclePercentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CirclePercentView, defStyleAttr, 0);
        //填写自定义属性里的色值，后面是默认属性
        mCircleColor = typedArray.getColor(R.styleable.CirclePercentView_circleBg, 0xff8e29fa);
        mArcColor = typedArray.getColor(R.styleable.CirclePercentView_arcColor, 0xffffee00);
        mArcWidth = typedArray.getDimensionPixelSize(R.styleable.CirclePercentView_arcWidth, DensityUtils.dp2px(context, 16));
        mPercentTextColor = typedArray.getColor(R.styleable.CirclePercentView_percentTextColor, 0xffffee00);
        mPercentTextSize = typedArray.getDimensionPixelSize(R.styleable.CirclePercentView_percentTextSize, DensityUtils.sp2px(context, 16));
        //可见getDimensionPixelSize和getDimensionPixelOffset的功能差不多,都是获取某个dimen的值,Offset中如果是dp或sp的单位,将其乘以density,如果是px,则不乘;size都乘以density
        mRadius = typedArray.getDimensionPixelSize(R.styleable.CirclePercentView_radius, 10);
        //用完记得释放
        typedArray.recycle();
        //配置画笔 画布等
        //ANTI_ALIAS_FLAG 抗锯齿?
        //圆形画笔
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);    //设置填充样式  stoke就是只描边线的颜色  fill是填充的颜色
        mCirclePaint.setColor(mCircleColor);
        //外圈圆
        mCircleoutPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCircleoutPaint.setStyle(Paint.Style.STROKE);    //设置填充样式  stoke就是只描边线的颜色  fill是填充的颜色
        mCircleoutPaint.setColor(Color.BLUE);
        mCircleoutPaint.setStrokeWidth(15);
        //外圈圆线画笔
        mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(mArcWidth);
        mArcPaint.setColor(mArcColor);
        mArcPaint.setStrokeCap(Paint.Cap.SQUARE);    //当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式  * Cap.ROUND,或方形样式Cap.SQUARE
        //文字画笔
        mPercentTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPercentTextPaint.setStyle(Paint.Style.STROKE);
        mPercentTextPaint.setColor(mPercentTextColor);
        mPercentTextPaint.setTextSize(mPercentTextSize);
        mArcRectF = new RectF();    //圆弧的外接矩形
        mTextBound = new Rect();   //文本范围矩形

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(CirclePercentView.this);
                }
            }
        });
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int eventaction = event.getAction();
                boolean mCirclereturn = false;
                switch (eventaction) {
                    /**当初始进来的时候 ，向下移动的状态*/
                    case MotionEvent.ACTION_DOWN:
                        mXdown = event.getX();
                        mYdown = event.getY();
                        /**balId>0,小球的移动状态*/
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        //添加圆内判断
                        Log.e("upX", mXdown + "");
                        Log.e("upY", mYdown + "");
                        Log.e("mRadius", mRadius + "");
                        Log.e("result", Math.sqrt(Double.parseDouble(String.valueOf(Math.pow(Double.parseDouble(String.valueOf(Math.abs(getWidth() / 2 - mXdown))), 2) + Math.pow(Double.parseDouble(String.valueOf(Math.abs(getHeight() / 2 - mYdown))), 2)))) + "");
                        if (Math.sqrt(Double.parseDouble(String.valueOf(Math.pow(Double.parseDouble(String.valueOf(Math.abs(getWidth() / 2 - mXdown))), 2) + Math.pow(Double.parseDouble(String.valueOf(Math.abs(getHeight() / 2 - mYdown))), 2)))) < mRadius) {
                            mCirclereturn = false;
                        } else {
                            mCirclereturn = true;
                        }
                        break;
                }

                return mCirclereturn;
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //直接设置在父控件里的宽高
        setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));
    }



    private int measureDimension(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //match parent
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 2 * mRadius;
            //wrap content
//            int a = specSize;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
//                mRadius = result;
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画圆
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mCirclePaint);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius - mArcWidth / 2, mCircleoutPaint);
        //?????
        //画矩形
        mArcRectF.set(getWidth() / 2 - mRadius
                        + mArcWidth / 2
                ,
                getHeight() / 2 - mRadius
                        + mArcWidth / 2
                ,
                getWidth() / 2 + mRadius
                        - mArcWidth / 2
                ,
                getHeight() / 2 + mRadius
                        - mArcWidth / 2
        );
        //画圆弧  第二个参数决定了起始点   第三个参数决定了是什么形状
        canvas.drawArc(mArcRectF, 270, 360 * mCurPercent / 100, true, mArcPaint);

        String text = mCurPercent + "%";
        //计算文本宽高
        mPercentTextPaint.getTextBounds(text, 0, String.valueOf(text).length(), mTextBound);
        //画百分比文本
        canvas.drawText(text, getWidth() / 2 - mTextBound.width() / 2
                , getHeight() / 2 + mTextBound.height() / 2, mPercentTextPaint);
    }
}
